﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBUtility
{

    /// <summary>
    /// 对图片进行压缩处理
    /// </summary>
    public class CImageLibrary
{
   public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }
   
   //检查图片大小 
   public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT)
   {
      byte[] bs = File.ReadAllBytes(file);
      
      double size = (bs.Length / 1024);
      //大于50KB 
      if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize;
      Image img = Image.FromFile(file);
      if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize;
      return ValidateImageResult.OK;
   }
        /// <summary>
        /// 改变图片的长宽高
        /// </summary>
        /// <param name="target_width">目标宽度</param>
        /// <param name="target_height">目标高度</param>
        /// <param name="img">需要处理的图片</param>
        /// <returns></returns>
   public static Bitmap ChangeImgWH(int target_width, int target_height,Image img)
   {
    System.Drawing.Image original_image = null;
    System.Drawing.Bitmap final_image = null;
    System.Drawing.Bitmap real_image = null;
    System.Drawing.Graphics graphic = null;
    System.Drawing.Graphics real_graphic = null;
    original_image = img;
    int original_width=img.Width;
    int original_height = img.Height;
    int new_width, new_height;
    float target_ratio = (float)target_width / (float)target_height;
    float image_ratio = (float)original_width / (float)original_height;

    if (target_ratio > image_ratio)
    {
        new_height = target_height;
        new_width = (int)Math.Floor(image_ratio * (float)target_height);
    }
    else
    {
        new_height = (int)Math.Floor((float)target_width / image_ratio);
        new_width = target_width;
    }
    new_width = new_width > target_width ? target_width : new_width;
    new_height = new_height > target_height ? target_height : new_height;
    real_image = new System.Drawing.Bitmap(original_width, original_height);  // 真实图片大小
    real_graphic = System.Drawing.Graphics.FromImage(real_image);
    real_graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    real_graphic.Clear(System.Drawing.Color.Transparent);
    real_graphic.DrawImage(original_image, 0, 0, original_width, original_height);

    final_image = new System.Drawing.Bitmap(target_width, target_height);//新建一个bmp图片 
    graphic = System.Drawing.Graphics.FromImage(final_image);//新建一个画板 
    graphic.Clear(System.Drawing.Color.Transparent);//清空一下画布 
    graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, target_width, target_height));
    int paste_x = (target_width - new_width) / 2;
    int paste_y = (target_height - new_height) / 2;
    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//设置高质量,低速度呈现平滑程度 
    graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);//在指定位置画图 
    return final_image;
}



   //按宽度比例缩小图片 
   public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)
   {
      Image imgOutput = imgSource;
      
      Size size = new Size(imgSource.Width, imgSource.Height);
      if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换 
      
      if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)
      {
         double rate = MAX_WIDTH / (double)imgSource.Width;
         
         if (imgSource.Height * rate > MAX_WIDTH)
         rate = MAX_WIDTH / (double)imgSource.Height;
         
         size.Width = Convert.ToInt32(imgSource.Width * rate);
         size.Height = Convert.ToInt32(imgSource.Height * rate);
         
         imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);
      }
      
      return imgOutput;
   }
   
   //按比例缩小图片 
   public static Image GetOutputSizeImage(Image imgSource, Size outSize)
   {
      Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);
      return imgOutput;
   }
   
   public static byte[] GetImageBytes(string imageFileName)
   {
      Image img = Image.FromFile(imageFileName);
      return GetImageBytes(img);
   }
   
   public static byte[] GetImageBytes(Image img)
   {
      if (img == null) return null;
      try
      {
         System.IO.MemoryStream ms = new MemoryStream();
         img.Save(ms, ImageFormat.Jpeg);
         byte[] bs = ms.ToArray();
         ms.Close();
         return bs;
      }
      catch { return null; }
   }
   
   public static Image FromBytes(byte[] bs)
   {
      if (bs == null) return null;
      try
      {
         MemoryStream ms = new MemoryStream(bs);
         Image returnImage = Image.FromStream(ms);
         ms.Close();
         return returnImage;
         
      }
      catch { return null; }
   }
}
}
